// 权限配置
export const rolePermissions = {
  admin: ['*'], // 管理员可以访问所有路由
  user: [ // 普通用户可以访问的路由
    '/',
    '/dashboard',
    '/products',
    '/products/list',
    '/products/category',
    '/sales',
    '/sales/orders',
    '/sales/analysis',
    '/inventory',
    '/inventory/stock',
    '/inventory/in',
    '/inventory/out',
    '/inventory/alert',
    '/inventory/operation',
    '/inventory/adjustment'
  ]
}

// 获取用户角色
export function getUserRole() {
  const userInfoStr = localStorage.getItem('userInfo')
  if (userInfoStr) {
    try {
      const userInfo = JSON.parse(userInfoStr)
      // 确保从userInfo中获取role，无论userId是什么类型
      return userInfo.role || 'user'
    } catch (error) {
      console.error('解析用户信息失败:', error)
      return 'user'
    }
  }
  return 'user'
}

// 检查用户是否有权限访问路由
export function hasPermission(role, route) {
  if (role === 'admin') return true
  
  const permissions = rolePermissions[role] || []
  return permissions.includes(route.path) || permissions.includes('*')
} 